package cn.stylefeng.guns.schedule.service;

import cn.stylefeng.guns.modular.info.mapper.LeagueMapper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @创建人 xiesm
 * @创建时间 2020/6/7
 * @描述
 */
@Service
public class AddLeagueService {
    @Autowired(required = false)
    private LeagueMapper leagueMapper;

    public static void main(String[] args) {

    }

    public void addLeague() throws IOException {
        String[] strs = sendHttp();
        List<String> matches = getFiled(strs, "arrMatch");
        List<String> seasons = getFiled(strs, "arrSeason");
        for (int i = 0; i < matches.size(); i++) {
            String match = matches.get(i);
            for (int j = 0; j < seasons.size(); j++) {

            }
        }
    }

    private List<String > getFiled(String[] strs, String filed) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < strs.length; i++) {
            if (strs[i].indexOf(filed) != -1) {
                list.add(strs[i]);
//                String[] matches = strs[i].split(",");
//                leagueMapper.insert(new League(Integer.valueOf(matches[3]), matches[2].replace("\"", "")));
            }
        }
        return list;
    }


    public String[] sendHttp() throws IOException {
        String url = "http://data.7m.com.cn/database/index_gb.htm";
        Document doc = Jsoup.connect(String.format(url))
                .ignoreContentType(true)
                .userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)")
                .timeout(5000)
                .get();
        return doc.select("body").select("script").get(10).html()
                .replace("\n", "")
                .replace("\t", "")
                .replace("\r", "")
                .replace(" ", "")
                .replace("var", "")
                .replace("[", "")
                .replace("]", "")
                .replace("'", "")
                .split(";");
    }

}
